<?php


namespace app\admin\controller\rid;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="出入库明细")
 */
class ReceiptIssueDetails extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\ReceiptIssueDetails();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();

            list($page, $limit, $where) = $this->buildTableParames();

            $where = [];
            if(!empty($param['tracking_numbercr']) && isset($param['tracking_numbercr']))
            {
                $where[] = [
                    ['tracking_numbercr', 'like', "%{$param['tracking_numbercr']}%"],
                ];
            }
            if(!empty($param['discrepancy']) && isset($param['discrepancy']))
            {
                $where[] = [
                    ['discrepancy', 'like', "%{$param['discrepancy']}%"],
                ];
            }
            if(!empty($param['order_head']) && isset($param['order_head']))
            {
                $where[] = [
                    ['order_head', 'like', "%{$param['order_head']}%"],
                ];
            }

            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                foreach ($list as $k => $v){
                    //商品名称
                    $goods = Db::name('goods')->where('id',$v['gid'])->field('title')->find();
                    
                    $v['good_name'] = $goods['title'];
                    if($v["receipt_issue_type"] == 2){
                        $goods_warehousing = Db::name("goods_warehousing")->where("receipt_no",$v["odd_numbers"])->find();
                        $v["discrepancy"] = $goods_warehousing["receipt_username"];
                        $admin = Db::name("system_admin")->where("id",$goods_warehousing["receipt_id"])->find();
                        $v["order_head"] = $admin["employee_name"];
                    }
                    $list[$k] = $v;
                }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }


    /**
    * @NodeAnotation(title="入库明细")
    */
    public function warehousing_details()
    {
        $list = $this->model->where("receipt_issue_type","in",'1,2,4')->select()->toArray();
        foreach ($list as $k => $v){
            //商品名称
            $goods = Db::name('goods')->where('id',$v['gid'])->field('title')->find();
            $v['good_name'] = $goods['title'];

            $list[$k] = $v;
        }
        $count = $this->model->count();
        return json([
            "code"=>1,
            "count"=>$count,
            "data"=>$list
        ]);
     
    }

     /**
    * @NodeAnotation(title="出库明细")
    */
    public function outbound_details()
    {
        $list = $this->model->where("receipt_issue_type","in",'3,5')->select()->toArray();
        foreach ($list as $k => $v){
            //商品名称
            $goods = Db::name('goods')->where('id',$v['gid'])->field('title')->find();
            $v['good_name'] = $goods['title'];

            $list[$k] = $v;
        }
        $count = $this->model->count();
        return json([
            "code"=>1,
            "count"=>$count,
            "data"=>$list
        ]);
    }

    //出入库详情
    public function receipt_issue_detailed_detail(){
        $param = $this->request->param();
        $row = Db::name("receipt_issue_detailed")->where("id",$param["id"])->find();
       
        if($row["receipt_issue_type"] == 2 || $row["receipt_issue_type"] == 3){
            //订单详情
            $order = Db::name("order")->where("courier_number",$row["tracking_numbercr"])->find();
         
            $admin = Db::name("system_admin")->where("id",$order["kahuna_id"])->find();
      
            //收款凭证
            if(!empty($order["collection_voucher"])){
                $order["collection_voucher"] = explode(",",$order["collection_voucher"]);
            }
            //未完结作废图片
            if(!empty($order["void_picture"])){
                $order["void_picture"] = explode(",",$order["void_picture"]);
            }
            if(!empty($order["employee_name"])){
                $order["new_employee_name"] = $admin["employee_name"];
            }else{
                $order["new_employee_name"] = "";
            }   
            $order_item = Db::name("order_item")->where("oid",$order["id"])->select()->toArray();
            $order["order_item"] = $order_item;
           
            $order["create_time"] = date("Y-m-d H:i:s",$order["create_time"]);

            $goods_warehousing = Db::name("goods_warehousing")->where("receipt_no",$row["odd_numbers"])->find();
            if(!empty($goods_warehousing)){
                $row["discrepancy"] = $goods_warehousing["receipt_username"];
                $admin = Db::name("system_admin")->where("id",$goods_warehousing["receipt_id"])->find();
                $row["order_head"] = $admin["employee_name"];
            }
          

            $row["order"] = $order;
        }elseif($row["receipt_issue_type"] == 4 || $row["receipt_issue_type"] == 5){
            //盘点详情
            $checklist = Db::name("checklist")->where("check_no",$row["odd_numbers"])->find();
            $admin = Db::name("system_admin")->where("id",$checklist["outbound_uid"])->find();
            if(!empty($admin["employee_name"])){
                $checklist["employee_name"] = $admin["employee_name"];
            }else{
                $checklist["employee_name"] ="";
            }
            $checklist["create_time"] = date("Y-m-d H:i:s",$checklist["create_time"]);
            $checklist_item = Db::name("checklist_item")->where("checklist_id",$checklist["id"])->select()->toArray();
            $checklist["checklist_item"] = $checklist_item;
            $row["checklist"] = $checklist;
        }elseif($row["receipt_issue_type"] == 1){
            //入库单详情
            $goods_warehousing = Db::name("goods_warehousing")->where("tracking_numberkd",$row["tracking_numbercr"])->find();
            if(!empty($goods_warehousing)){
                $goods_warehousing_item = Db::name("goods_warehousing_item")->where("goods_warehousing_id",$goods_warehousing["id"])->select()->toArray();
                $goods_warehousing["goods_warehousing_item"] = $goods_warehousing_item;
                
                $row["goods_warehousing_item"] = $goods_warehousing;
            }else{
                $goods_warehousing_item = "";
                $goods_warehousing["goods_warehousing_item"] = $goods_warehousing_item;
                
                $row["goods_warehousing_item"] = $goods_warehousing;
            } 
        }
        
        $row["create_time"] = date("Y-m-d H:i:s",$row["create_time"]);
       
        
        return json(["code"=>1,"row"=>$row]);
    }
 
} 